# Maintainer: Chris Gorman <chrisjohgorman@gmail.com>

_realname=libiio
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.26
pkgrel=1
pkgdesc="Interface to the Linux Industrial Input/Output (IIO) Subsystem (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_repository_url='https://github.com/analogdevicesinc/libiio/'
url='https://analogdevicesinc.github.io/libiio/'
license=(
  'spdx:GPL-2.0-or-later OR LGPL-2.1-or-later'
)
depends=("${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-libserialport"
             "${MINGW_PACKAGE_PREFIX}-libusb"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://github.com/analogdevicesinc/libiio/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-Install-man-pages-to-the-correct-chapter-directories.patch")
sha256sums=('fb445fb860ef1248759f45d4273a4eff360534480ec87af64c6b8db3b99be7e5'
            'f20cf6c6eacd913a85df35a0188a657858c38826fc48a01d8f2afb82e31d43ac')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/0001-Install-man-pages-to-the-correct-chapter-directories.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  LDFLAGS="$LDFLAGS -lpthread" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

check() {
  "${MINGW_PREFIX}"/bin/ctest.exe --test-dir "build-${MSYSTEM}" --output-on-failure || warning "Tests failed"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.txt"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING_GPL.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING_GPL.txt"
}
